#include <iostream>
#include <stdlib.h>
#include<time.h>
void swap(int& a, int& b)
{

	int temp = a;
	a = b;
	b = temp;
}
int main()
{
	int n, m;
	std::cin >> n >> m;
	int** ar = new int* [n];
	for (int i = 0; i < n; i++)
	{
		ar[i] = new int[m];
	}
	srand(time(nullptr));
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			ar[i][j] = rand();
		}
	}
		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j < m; j++)
			{
				std::cout << ar[i][j] << "  ";

			}
			std::cout << std::endl;
		}
		std::cout << std::endl; std::cout << std::endl;
		
		if (n == m) {
			for (int i = 1; i < n; i++)
			{
				for (int j = 1; j < m; j++)
				{
					if (i == j) {
						for (int l = 1; l <= i; l++)
						{
							swap(ar[i][i - l], ar[i - l][i]);
						}
					}

				}
			}
			for (int i = 0; i < n; i++)
			{
				for (int j = 0; j < m; j++)
				{
					std::cout << ar[i][j] << "  ";

				}
				std::cout << std::endl;
			}
		}
		else
		{
			for (int i = 0; i < m; i++)
			{
				for (int j = 0; j < n; j++)
				{
					std::cout  << ar[j][i] << " ";
				}
				std::cout << std::endl;
			}
			return 0;
		}
		delete[]ar;
	}